package com.nifostasky.comic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nifostasky.comic.entitys.SysOrg;
import com.nifostasky.comic.service.SysOrgService;
import com.nifostasky.comic.vos.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 * 字典目录表 前端控制器
 * </p>
 *
 * @author liujun
 * @since 2021-07-02
 */
@Controller
@RequestMapping("/test")
@RestController
@Api(tags = "测试")
@ApiSort(1)
public class TestController {

    @Autowired
    private SysOrgService sysOrgService;

    @GetMapping("/test")
    @ApiOperation("test")
    @ApiOperationSupport(order = 5)
    public JsonResult tset() {
        //1. 获取所有行政区域
        List<SysOrg> list = sysOrgService.list();
        //2. sort排序赋值 根据原有的org_code(original_code)
        Map<Long, List<SysOrg>> map = new TreeMap<>();
        list.forEach(data->{
            if (!map.containsKey(data.getParentId())) {
                List<SysOrg> tmp = new ArrayList<>();
                map.put(data.getParentId(), tmp);
            }
            List<SysOrg> orgList = map.get(data.getParentId());
            //3. 设置
            data.setSort(orgList.size() + 1);
            orgList.add(data);
        });
        List<SysOrg> result = new ArrayList<>();
        for(Map.Entry<Long, List<SysOrg>> entry : map.entrySet()){
            result.addAll(entry.getValue());
        }
        sysOrgService.updateBatchById(result);
//        4. code赋值
        List<SysOrg> root = map.get(0);
        root.stream().sorted(new Comparator<SysOrg>() {
            @Override
            public int compare(SysOrg o1, SysOrg o2) {
                return Integer.valueOf(o1.getOrgCode()) - Integer.valueOf(o2.getOrgCode());
            }
        });
        for(int i = 0;i<root.size();i++){
            root.get(i).setOrgCode("."+(i+1)+".");
        }
        //入库保存然后重新迭代查询
        return new JsonResult(map);
    }

//    @GetMapping("/test1")
//    @ApiOperation("test1")
//    @ApiOperationSupport(order = 5)
////    public JsonResult test() {
////        List<SysOrg> child = findChild(0L);
////        if(null != child){
////            child.forEach(data->{
////                data.setOrgCode();
////            });
////        }
////        QueryWrapper<SysOrg> qw = new QueryWrapper<>();
////        List<SysOrg> list = sysOrgService.list();
////        //4. code赋值
////
////    }
//
//    private List<SysOrg> findChild(Long parentId){
//        QueryWrapper<SysOrg> qw = new QueryWrapper<>();
//        qw.eq("parent_id",parentId);
//        List<SysOrg> list = sysOrgService.list(qw);
//        return list;
//    }

    @GetMapping("/test3")
    @ApiOperation("test3")
    @ApiOperationSupport(order = 6)
    public JsonResult test3() {
        //1. 获取所有行政区域
        List<SysOrg> list = sysOrgService.list();
        //2. sort排序赋值 根据原有的org_code(original_code)
        Map<Long, List<SysOrg>> map = new TreeMap<>();
        list.forEach(data->{
            if (!map.containsKey(data.getParentId())) {
                List<SysOrg> tmp = new ArrayList<>();
                map.put(data.getParentId(), tmp);
            }
            List<SysOrg> orgList = map.get(data.getParentId());
            orgList.add(data);
        });

        List<SysOrg> result = new ArrayList<>();
        for(Map.Entry<Long, List<SysOrg>> entry : map.entrySet()){
            // 根据code查询其ID
            QueryWrapper<SysOrg> qw = new QueryWrapper<>();
            qw = qw.eq("org_code", entry.getKey());
            SysOrg one = sysOrgService.getOne(qw);
            if(null != one){
                if(null != entry.getValue()){
                    entry.getValue().forEach(data->{
                        data.setParentId(one.getId());
                    });
                }
                result.addAll(entry.getValue());
            }
        }
        sysOrgService.updateBatchById(result);
        return  new JsonResult(result);
    }
}

